package org.platform.processor.diagnose.job;

import com.mc.mic.core.log.bus.BusLogPusher;
import com.mc.mic.core.log.frame.LogBo;
import lombok.extern.slf4j.Slf4j;
import org.platform.constant.LogType;
import org.platform.dao.mapper.FdStepInfoMapper;
import org.platform.dao.mapper.FdTaskProcessDataMapper;
import org.platform.mo.bussmo.log.TaskStepMqBO;
import org.platform.mo.bussmo.task.TaskExecutorStep;
import org.platform.mo.csmo.common.StepInfo;
import org.platform.mo.csmo.common.TaskProcessData;
import org.platform.mo.db.FdStepInfo;
import org.platform.mo.db.FdTaskProcessData;
import org.platform.tool.util.BeansUtils;
import org.platform.tool.util.ComUtils;
import org.quartz.InterruptableJob;
import org.quartz.JobExecutionContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 保存任务步骤信息,并且同时推送到客服端
 *
 * @author FengJie
 * @date 2018/7/20
 */
@Slf4j
public class PromoteTaskExecutorStepJob implements InterruptableJob {
    private static final String DATA_KEY = "TaskProcessData";

    @Autowired
    private FdTaskProcessDataMapper taskProcessDataMapper;
    @Autowired
    private FdStepInfoMapper stepInfoMapper;
    @Autowired
    private BusLogPusher mqLog;

    @Override
    public void interrupt() {
    }

    @Override
    public void execute(JobExecutionContext context) {
        try {
            TaskExecutorStep.TaskProcessData taskProcessData =
                    (TaskExecutorStep.TaskProcessData) context.getMergedJobDataMap().get(DATA_KEY);
            TaskProcessData data = saveOrUpdate(taskProcessData);
            promote(data);
        } catch (Exception e) {
            log.error("处理【任务执行步骤】发生错误,异常信息", e);
        }
    }

    private TaskProcessData saveOrUpdate(TaskExecutorStep.TaskProcessData taskProcessData) {
        String taskSerialNumber = taskProcessData.getTaskSerialNumber();
        FdTaskProcessData data = new FdTaskProcessData();
        BeanUtils.copyProperties(taskProcessData, data);
        data.setId(taskSerialNumber);

        List<FdStepInfo> infoList = new ArrayList<>(taskProcessData.getStepsList().size());
        for (TaskExecutorStep.StepInfo stepInfo : taskProcessData.getStepsList()) {
            FdStepInfo info = new FdStepInfo();
            BeanUtils.copyProperties(stepInfo, info);
            info.setId(ComUtils.getUUID());
            info.setTaskProcessId(taskSerialNumber);
            infoList.add(info);
        }

        FdTaskProcessData processData = taskProcessDataMapper.selectByPrimaryKey(taskSerialNumber);
        if (null == processData) {
            taskProcessDataMapper.insert(data);
            infoList.forEach(info -> stepInfoMapper.insert(info));
        } else {
            Example example = new Example(FdTaskProcessData.class);
            example.createCriteria()
                    .andEqualTo("taskSerialNumber", taskSerialNumber);
            taskProcessDataMapper.updateByExampleSelective(data, example);
            infoList.forEach(info -> stepInfoMapper.updateByExampleSelective(info, example));
        }
        TaskProcessData tpd = new TaskProcessData();
        BeanUtils.copyProperties(data, tpd);
        List<StepInfo> stepInfoList = BeansUtils.copyListProperties(infoList, StepInfo.class);
        tpd.setStepInfoList(stepInfoList);
        return tpd;
    }

    /**
     * 推送到客户端
     *
     * @param taskProcessData
     */
    private void promote(TaskProcessData taskProcessData) {
        mqLog.log(new LogBo<>(LogType.TASK_STEP, new TaskStepMqBO(taskProcessData)));
    }
}
